/**
 * 资产管理
 */
Ext.define('Module.zc.Zcgl', {
	extend : 'Ext.panel.Panel',
	layout: 'fit',
	config:{
		pid:''
	},
	initComponent : function() {
		var me = this;
		
		var treeStroe = Ext.create('Ext.data.TreeStore', {
			fields: ['id','leaf','name','code','ms','pid','sbid','isLeaf','sjid'],
		        proxy: {
		            type: 'ajax',
		            url: 'zcglAction/getZcglTree.do'
		        },
		        root: {
		        	expanded: true,  
		            id: '0'
		        }
		    });
		
		var menutree = me.menutree =  Ext.create('Ext.tree.Panel',{
			title: '资产管理',
			rootVisible: false,
			border: false,
			width: 450,
			region: 'west',
			tbar: [{
				text: '添加工作站',
				iconCls: 'icon-add',
				handler: function(){
					me.addZcgl(null,'addZcgl.do');
					Ext.getCmp(me.id+'-winForm').setTitle('添加工作站');
				}
			},{
				text: '添加设备',
				iconCls: 'icon-add',
				handler: function(){
					
					var rec = menutree.getSelectionModel().getSelection()[0];
					if(rec){
						me.addZcgl(null,'addZcgl.do');
							Ext.getCmp(me.id+'-pid').setValue(rec.data.pid);
							Ext.getCmp(me.id+'-leaf').setValue(2);
					}
					else{
						Ext.Msg.alert('提示', '请选择设备目录。');
					}
				}
			},{
				text: '修改数据',
				iconCls: 'icon-edit',
				handler: function(){
					var rec = menutree.getSelectionModel().getSelection()[0];
					if(rec && (rec.data.leaf !=1 || rec.data.isLeaf =='str')){
						me.addZcgl(rec,'updateZcgl.do');
						
						Ext.getCmp(me.id+'-winForm').setIconCls('icon-edit');
						if(rec.data.sbid !=''){
							Ext.getCmp(me.id+'-id').setValue(rec.data.sbid);
						}
						if(rec.data.leaf !=2){
							Ext.getCmp(me.id+'-leaf').setValue('');
						}
					}
					else{
						Ext.Msg.alert('提示', '请选择要修改的数据。');
					}
				}
			},{
				text: '删除数据',
				iconCls: 'icon-delete',
				handler: function(){
					var rec = menutree.getSelectionModel().getSelection()[0];
					if(rec){
						me.delZcgl(rec);
					}
					else{
						Ext.Msg.alert('提示', '请选择要删除的数据。');
					}
				}
			}],
			store: treeStroe,
		    refresh: function(nodeid){
		    	if(nodeid){//根据父节点刷新子节点数据
		    		var root = menutree.getRootNode();
		    		var node = root.findChild('id',nodeid,true);
		    		node.removeAll();
		    		node.expand();
		    		Ext.Ajax.request({
		    			async: true,
		    			method: 'POST',
		    			params: {
		    				node: nodeid,
		    				leaf:node.data.leaf
		    			},
		    			url:  'zcglAction/getZcglTree2.do', 
		    			success: function(response, options) {
		    				var obj = Ext.decode(response.responseText);
		    				Ext.Array.each(obj,function(_data){
		    					var _node = node.appendChild(Ext.clone(_data));
		    					_node.raw = _data;
		    				});
		    			},
		    			failure: function(response, options){
		    				return response;
		    			}
		    		});
		    	}else{//刷新父节点数据
		    		treeStroe.reload();
		    	}
        			
		    },
			columns: [{
				xtype: 'treecolumn', 
	            text: '设备名称',
	            width: 220,
	            sortable: false,
	            dataIndex: 'name'
			},{
	            text: '设备编号',
	            width: 150,
	            dataIndex: 'code'
			},{
	            text: '设备类型',
	            width: 200,
	            dataIndex: 'leaf',
	            renderer: function(v,met,rec){
	            	switch (v) {
					case 2: return '采集工作站';
					case true: return '采集终端';
					}
	            }
			},{
	            text: '描述',
	            width: 400,
	            dataIndex: 'ms'
			}]
		});
		
		
		me.defaults = {padding: 1};
		me.items = [menutree];
		
		
		me.callParent();
	},
	addZcgl: function(rec,url){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 450,
			height: 220,
			iconCls: 'icon-add',
			id:me.id+'-winForm',
			modal: true,
			layout: 'fit',
			title: rec ? '编辑设备': "添加设备",
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('window').down('form');
        	    	var form = formp.form;
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'zcglAction/'+url,
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		me.menutree.getSelectionModel().deselectAll(true); 
            	    	    		var pid=form.getValues().pid;
            	    	    		var isLeaf=null;
            	    	    		if(url=='updateZcgl.do'){
            	    	    			pid=rec.data.pid;
            	    	    			isLeaf=rec.data.isLeaf;
            	    	    			if(isLeaf !='str'){
            	    	    				me.menutree.refresh(null);
            	    	    			}else{
            	    	    				me.menutree.refresh(pid);
            	    	    			}
            	    	    		}else{
            	    	    			me.menutree.refresh(pid);
            	    	    		}
            	    	    		
            	    	    		
            	    	    		formp.up('window').close();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						if(rec){
							fp.form.setValues(rec.raw);
						}
					}
				}
			})
		}).show();
	},
	delZcgl: function(rec){
		var me = this;
		var id = null;
		if(rec.data.sbid !=''){
			id = rec.data.sbid;
   		}else{
   			id = rec.data.id;
   		}
		
			Ext.Msg.confirm('提示', '你确定要删除当前数据吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'zcglAction/deleteZcgl.do',
					    params: {
					        id: id,
					        leaf: rec.data.leaf,
					        name: rec.data.name
					    },
					    success: function(response){
					    	var rs = Ext.decode(response.responseText);
	       	    	    	if(rs.rst){
	       	    	    		Ext.Msg.alert('提示', '删除成功！');
	       	    	    		me.menutree.getSelectionModel().deselectAll(true); 
	       	    	    		if(rec.data.leaf==2){
	       	    	    			me.menutree.refresh(null);
	       	    	    		}else{
	       	    	    			me.menutree.refresh(rec.data.pid);
	       	    	    		}
	       	    	    	}
	       	    	    	else{
	       	    	    		Ext.Msg.alert('提示', '删除失败！');
	       	    	    	}
					    }
					});
				}
			});
	},
	getForm: function(config){
		var me = this;
		var formp = Ext.create('Ext.form.Panel',Ext.apply({
			bodyPadding: 10,
			defaults: {anchor: '100%',},
			items: [{
					name: 'id',
					xtype: 'hidden',
					id:me.id+'-id'
				},{
					name: 'pid',
					xtype: 'hidden',
					id:me.id+'-pid'
				},{
					xtype: 'hidden',
					id:me.id+'-isUpSb'
				},{
					name: 'leaf',
					xtype: 'hidden',
					id:me.id+'-leaf'
				},{
					fieldLabel: '设备编号',
					name: 'code',
					id:me.id+'-code',
					xtype: 'textfield',
					maxLength:20,
					allowBlank: false
				},{
					fieldLabel: '设备名称',
					name: 'name',
					id:me.id+'-name',
					xtype: 'textfield',
					maxLength:20,
					allowBlank: false
				},{
					fieldLabel: '描述',
					name: 'ms',
					id:me.id+'-ms',
					maxLength:100,
					xtype: 'textarea'
				}]
		},config));
		
		return formp;
	}
});